/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2019/1/19
 * Time: 23:41
 */
$(function () {
    var id = $('.department-id').val();
    var ue = UE.getEditor('editor', {initialFrameHeight: 250});

    sendAjax('/v1/manager/department/detail.json', 'get', {department_id: id}, function (json) {
        $('.department-name').text(json.body.name);
        var members = '';
        $.each(json.body.members, function (n, v) {
            members += v.realname + '，';
        });
        if (members == '') {
            members = '';
        } else {
            members = members.slice(0, -1);
            if (json.body.members.length >= 5) {
                members += '...<a href="#" data-toggle="modal" data-target="#department-members-detail">全部</a>';
            }
        }
        $('.department-members').html(members);
        $('.department-found-time').text(json.body.found_at);
        $('.department-desc').text(json.body.desc);
        $('.department-score').text(json.body.score);
        var star = '';
        var count = 0;
        if (json.body.score >= 90) {
            count = 5;
        } else if (json.body.score >= 60) {
            count = 3;
        } else {
            count = 0;
        }
        for (var i = 0; i < count; i ++) {
            star += '<i class="fa fa-star"></i>';
        }
        $('.department-star').html(star);
        $('.department-summary-text').val(json.body.summary);
    });

    var url = '/v1/manager/advice/to/department.json';
    var data = {
        department_id : id,
        year : 0,
        page : 1,
        page_size : 10
    };
    getDatas(url, data);

    $('.select-year').on('change', function () {
        data.year = $(this).val();
        data.page = 1;
        getDatas(url, data);

    });

    $(document).on('click', '.page-list a', function(){
        data.year = $('.select-year').val();
        data.page = $(this).attr('data-id');
        getDatas(url, data);
    });

    $('#normal-dialog').on('show.bs.modal', function () {
        $(this).find('.modal-header .modal-title').text('查看党支部自我评价');
        $(this).find('.modal-body').html('<div>'+$('.department-summary-text').val()+'</div>');
        $(this).find('.modal-footer .btn-primary').addClass('hidden');
    }).on('hide.bs.modal', function () {
        $(this).find('.modal-header .modal-title').text('普通弹窗');
        $(this).find('.modal-body').html('');
        $(this).find('.modal-footer .btn-primary').removeClass('hidden');
    });

    $('#department-members-detail').on('show.bs.modal', function () {
        $(this).find('.modal-title').text('党支部成员');
        $(this).find('.modal-footer .dialog-btn-save').addClass('hidden');
        var modal_body = $(this).find('.modal-body');
        sendAjax('/v1/manager/department/users.json', 'get', {department_id: id}, function(json){
            var data = json.body;

            if (data.length > 0) {
                var html = '<div class="form-horizontal"><div class="form-group"><label class="col-sm-3 control-label">所有成员：</label><div class="col-sm-9">';
                $.each(data, function(n, v){
                    html += '<span class="real-name" style="padding-right: 20px">' + v.realname + '</span>';
                });
                html += '</div></div></div>';
                modal_body.html(html);
            }
        });
    }).on('hide.bs.modal', function () {
        $(this).find('.modal-body').html('');
    });

    $('#manager-advice').on('show.bs.modal', function () {


    }).on('hide.bs.modal', function(){
        ue.setContent('');
    });

    $('.dialog-btn-save').on('click', function () {
        $('.advice-content').val(ue.getContent());
        var params = {
            department_id: id,
            content: $('.advice-content').val()
        };
        saveData('/v1/manager/save/advice/to/department', {params: params}, '');
    });
});



function getDatas(url, data) {
    sendAjax(url, 'get', data, function (json) {
        var html = '';
        var page_html = '';
        if (json.rows.length > 0) {
            $.each(json.rows, function (n, v) {
                html += '<div class="row"><div class="col-xs-12 data-detail">';
                html += '<span>党委：</span><span class="department-name"></span>';
                html += '<span>&nbsp;时间：</span><span class="create-time">'+v.created_at+'</span></div>';
                html += '<div class="col-xs-12"><strong>意见内容：</strong></div>';
                html += '<div class="col-xs-12"><p class="advice-content">'+v.content+'</p></div></div>';
            });
            page_html = pageList(data.page, data.page_size, json.total, json.pages, json.rows.length);
        }
        $('.data-list').html(html);
        $('.page-list').html(page_html);
    });
}